package com.sunwayworld.basemodule.business.qualitycontrol.controlmaterial.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.sunwayworld.basemodule.business.qualitycontrol.controlmaterial.bean.LimsControlmaterialBean;
import com.sunwayworld.basemodule.business.qualitycontrol.controlmaterial.dao.LimsControlmaterialDao;
import com.sunwayworld.basemodule.business.qualitycontrol.controlmaterial.service.LimsControlmaterialService;
import com.sunwayworld.basemodule.business.qualitycontrol.qualitycontrolplan.bean.LimsQualitycontrolplanBean;
import com.sunwayworld.basemodule.business.qualitycontrol.qualitycontrolplan.service.LimsQualitycontrolplanService;
import com.sunwayworld.framework.at.annotation.AuditTrailEntry;
import com.sunwayworld.framework.at.annotation.AuditTrailType;
import com.sunwayworld.framework.context.ApplicationContextHelper;
import com.sunwayworld.framework.mybatis.mapper.MatchPattern;
import com.sunwayworld.framework.mybatis.mapper.SearchFilter;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;
import com.sunwayworld.framework.spring.annotation.GikamBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
@GikamBean
public class LimsControlmaterialServiceImpl implements LimsControlmaterialService {

    @Autowired
    private LimsControlmaterialDao limsControlmaterialDao;

    @Override
    @SuppressWarnings("unchecked")
    public LimsControlmaterialDao getDao() {
        return limsControlmaterialDao;
    }

    @Autowired
    private LimsQualitycontrolplanService limsQualitycontrolplanService;

    @Override
    @Transactional
    @AuditTrailEntry(AuditTrailType.INSERT)
    public Long insert(RestJsonWrapperBean jsonWrapper) {
        LimsControlmaterialBean limsControlmaterial = jsonWrapper.parseUnique(LimsControlmaterialBean.class);
        limsControlmaterial.setId(ApplicationContextHelper.getNextIdentity());
        limsControlmaterial.setActiveStatus("on");
        getDao().insert(limsControlmaterial);
        return limsControlmaterial.getId();
    }

    @Override
    public JSONObject checkIfStillInUse(RestJsonWrapperBean bean) {
        List<LimsControlmaterialBean> controlmaterialBeans = bean.parse(LimsControlmaterialBean.class);
        JSONObject req = new JSONObject();
        boolean flag = false;
        StringBuffer message = new StringBuffer();

        controlmaterialBeans.forEach(i -> {
            LimsQualitycontrolplanBean first = limsQualitycontrolplanService.selectFirstByFilter(SearchFilter.instance().match("controlMaterialId", i.getId()).filter(MatchPattern.EQ));
            if (first != null) {
                message.append(i.getControlMaterialName()).append("<br/>");
            }
        });

        if (message.length() > 0) {
//            message.insert(0, I18nHelper.getMessage("QUALITY_CONTROL.PLANPROJECT.CANNOT_DELETE") + "<br/>");
            flag = true;
        }

        req.put("flag", flag);
        req.put("message", message);
        return req;
    }
}
